var pageIndex;
$(function() {
	$("#FollowQuestionViewFrom").validate({
		rules : {
			'questionCategoryId' : {
			 	required:true
			},
			'questionTitle' : {
			 	required:true
			}
		},
		submitHandler : function(form) {
			var options = {
				dataType : "json",
				success : function(data) {
					if (data.success) {
						$.success('操作成功', function() {
							layer.close(pageIndex);
							window.location.href = "getFollowQuestionList";
						});
					} else {
						$.error(data.msg);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.warn('服务忙,请稍后再试.' + textStatus + errorThrown);
				}
			};
			$(form).ajaxSubmit(options);
		},
		invalidHandler : function(form, validator) {
			return false;
		}
	});

})

function add(){
	var html = $("#follow_question_template").html();
	var count = $("#incomeData").find('.rows').length;
	if(count==26){
		$.warn("最多支持26个选项");
		return ;
	}
	html = template('follow_question_template', {
		index : count,
		countNum: count+1,
		sort: String.fromCharCode(64+count+1)
	});
	$("#incomeData").append(html);
	
}
function del(obj){

	var  p =$(obj).parent('.form-group').parent('.rows');
	p.remove();
	var siblings =$("#incomeData").find('.rows');
	siblings.each(function(index, element) {
		var i = index;
		var answerFounds = $(element).find("[name^='answer']");
		answerFounds.each(function() {
			$(this).attr(
					'name',
					$(this).attr('name').replace(/answer\[\d+]/g,
							'answer[' + index + ']'));
			
		});
		var option =index+1 ;
		var countNum = $(element).find("[id^='answer']");
		countNum.html("选项 "+option+":");
	
		var letter = $(element).find("[id^='sort']");
		$(letter).val(String.fromCharCode(64+option));
		
	});
}